package com.meme.util;

import java.util.List;
import java.util.LinkedList;
import java.util.Hashtable;

public class ObjectCollector <T> 
{
	private List<T> mObjectList = new LinkedList<T> ();
	private Hashtable<T, Integer> mIndexLookupTable = new Hashtable<T, Integer> ();
	
	
	public int addObject (T object)
	{
		int index = getObjectIndex (object);
		if (index < 0)
		{
			mIndexLookupTable.put(object, mObjectList.size());
			mObjectList.add (object);
			index = mObjectList.size();
		}
		
		return index;
	}
	
	public int getObjectIndex (T object)
	{
		Integer integer = mIndexLookupTable.get (object);
		if (integer == null)
			return -1;
		else
			return integer.intValue();
	}
	
	public int getObjectsCount ()
	{
		return mObjectList.size();
	}
	
	public T getObjectAt (int pos)
	{
		return mObjectList.get (pos);
	}
	
	public List<T> getObjectList ()
	{
		List<T> list = new LinkedList<T> ();
		list.addAll (mObjectList);
		return list;
	}
}
